package com.gosophia.conferenceManagement.starting.test.integration;

import java.util.List;

import junit.framework.Assert;

import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.transaction.annotation.Transactional;

import com.gosophia.commons.DateTranslate;
import com.gosophia.commons.exception.BusinessException;
import com.gosophia.conferenceManagement.starting.entity.ConferenceStartingTransientStore;
import com.gosophia.conferenceManagement.starting.service.ConferenceTransientStoreService;
import com.gosophia.dataDictionary.entity.DataValue;
import com.gosophia.dataDictionary.service.DataDictionaryService;
import com.gosophia.userManagement.entity.UserInfo;
import com.gosophia.userManagement.service.UserManagementService;

@ContextConfiguration(locations = { "classpath*:moduleContext.xml",
		"classpath:applicationContext-it.xml" })
@Transactional(rollbackFor = { BusinessException.class })
public class ITConferenceTransientStoreServiceImpl extends
		AbstractTransactionalJUnit4SpringContextTests {

	@Autowired
	private ConferenceTransientStoreService conferenceTransientStoreService;

	@Autowired
	private UserManagementService userManagementService;
	@Autowired
	private DataDictionaryService dataDictionaryService;

	@Autowired
	private SessionFactory sf;

	/**
	 * 测试saveTransientInfo()方法
	 */
	@Test
	public void testSaveTransientInfo() throws BusinessException {
		ConferenceStartingTransientStore cts = new ConferenceStartingTransientStore();
		cts.setApplicationExplanation("applicationExplanation");
		cts.setApplicationInfo("applicationInfo");
		UserInfo u = userManagementService.findUserInfoByUserInfoId(1L);
		cts.setConferenceCreator(u);
		cts.setConferenceDescription("conferenceDescription");
		cts.setConferenceModel("conferenceModel");
		cts.setConferenceName("conferenceName");
		cts.setConferenceType("conferenceType");
		UserInfo c = userManagementService.findUserInfoByUserInfoId(2L);
		cts.setConfernceUnderTaker(c);
		DataValue i = dataDictionaryService.findDataValueByDataValueId(1012L);
		cts.setImportantLevel(i);
		cts.setMajorIssue("majorIssue");
		cts.setMajorParticipators("majorParticipators");
		UserInfo n = userManagementService.findUserInfoByUserInfoId(3L);
		cts.setNextApproval(n);
		cts.setPlanEndTime(DateTranslate.simpleStringToDate("2011-11-11"));
		cts.setPlanStartTime("planStartTime");
		ConferenceStartingTransientStore test = conferenceTransientStoreService
				.saveTransientInfo(cts);

		Assert.assertEquals("applicationExplanation", test
				.getApplicationExplanation());
	}

	/**
	 * 测试updateTransientInfo()方法
	 * 
	 * @throws BusinessException
	 */
	@Test
	public void testUpdateTransientInfo() throws BusinessException {
		ConferenceStartingTransientStore cts = new ConferenceStartingTransientStore();
		cts.setApplicationExplanation("applicationExplanation");
		cts.setApplicationInfo("applicationInfo");
		UserInfo u = userManagementService.findUserInfoByUserInfoId(1L);
		cts.setConferenceCreator(u);
		cts.setConferenceDescription("conferenceDescription");
		cts.setConferenceModel("conferenceModel");
		cts.setConferenceName("conferenceName");
		cts.setConferenceType("conferenceType");
		UserInfo c = userManagementService.findUserInfoByUserInfoId(2L);
		cts.setConfernceUnderTaker(c);
		DataValue i = dataDictionaryService.findDataValueByDataValueId(1012L);
		cts.setImportantLevel(i);
		cts.setMajorIssue("majorIssue");
		cts.setMajorParticipators("majorParticipators");
		UserInfo n = userManagementService.findUserInfoByUserInfoId(3L);
		cts.setNextApproval(n);
		cts.setPlanEndTime(DateTranslate.simpleStringToDate("2011-11-11"));
		cts.setPlanStartTime("planStartTime");
		ConferenceStartingTransientStore test = conferenceTransientStoreService
				.saveTransientInfo(cts);

		ConferenceStartingTransientStore testUpdate = new ConferenceStartingTransientStore();
		testUpdate.setApplicationExplanation("testApplicationExplanation");
		testUpdate.setTransientStoreId(test.getTransientStoreId());
		ConferenceStartingTransientStore testUpdateBack = conferenceTransientStoreService
				.updateTransientInfo(testUpdate);
		Assert.assertEquals("testApplicationExplanation", testUpdateBack
				.getApplicationExplanation());
		Assert.assertEquals("applicationInfo", testUpdateBack
				.getApplicationInfo());
	}

	@SuppressWarnings("unchecked")
	@Test
	public void testDeleteTransientInfo() throws BusinessException {
		ConferenceStartingTransientStore cts = new ConferenceStartingTransientStore();
		cts.setApplicationExplanation("applicationExplanation");
		cts.setApplicationInfo("applicationInfo");
		UserInfo u = userManagementService.findUserInfoByUserInfoId(1L);
		cts.setConferenceCreator(u);
		cts.setConferenceDescription("conferenceDescription");
		cts.setConferenceModel("conferenceModel");
		cts.setConferenceName("conferenceName");
		cts.setConferenceType("conferenceType");
		UserInfo c = userManagementService.findUserInfoByUserInfoId(2L);
		cts.setConfernceUnderTaker(c);
		DataValue i = dataDictionaryService.findDataValueByDataValueId(1012L);
		cts.setImportantLevel(i);
		cts.setMajorIssue("majorIssue");
		cts.setMajorParticipators("majorParticipators");
		UserInfo n = userManagementService.findUserInfoByUserInfoId(3L);
		cts.setNextApproval(n);
		cts.setPlanEndTime(DateTranslate.simpleStringToDate("2011-11-11"));
		cts.setPlanStartTime("planStartTime");
		ConferenceStartingTransientStore test = conferenceTransientStoreService
				.saveTransientInfo(cts);

		conferenceTransientStoreService.deleteTransientInfo(test);
		sf.getCurrentSession().clear();

		List<ConferenceStartingTransientStore> back = sf
				.getCurrentSession()
				.createQuery(
						"from ConferenceStartingTransientStore c where c.conferenceCreator.userInfoId = 1001L")
				.list();

		Assert.assertEquals(0, back.size());
	}

	@Test
	public void testFindTransientInfoByCreator() {
		ConferenceStartingTransientStore test = conferenceTransientStoreService
				.findTransientInfoByCreator(1016L);

		Assert.assertEquals("从业务进入发起申请的测试", test.getConferenceName());
	}

	@Test
	public void testFindTransientInfoByJobId() {
		ConferenceStartingTransientStore test = conferenceTransientStoreService
				.findTransientInfoByJobId(1L);
		Assert.assertEquals("从任务进入发起会议任务测试", test.getConferenceName());
	}

	public void testFindTransientInfoById() {
		ConferenceStartingTransientStore test = conferenceTransientStoreService
				.findTransientInfoById(2L);
		Assert.assertEquals("从任务进入发起会议任务测试", test.getConferenceName());
	}

}
